﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;


namespace TrainTicketsRobot
{
    class TRConvert
    {
        public static OrderWaitTime ToOrderWaiteTime(string strJson)
        {
            try
            {
                if (strJson == null || strJson == "")
                {
                    return null;
                }
                
                if (!strJson.Contains("\"tourFlag\":"))
                {
                    strJson = strJson.Remove(strJson.Length - 1);
                    strJson = strJson + ",\"tourFlag\":\"\"}";
                }
                
                if (!strJson.Contains("\"waitCount\":"))
                {
                    strJson = strJson.Remove(strJson.Length - 1);
                    strJson = strJson + ",\"waitCount\":-999}";
                }
                if (!strJson.Contains("\"orderId\":"))
                {
                    strJson = strJson.Remove(strJson.Length - 1);
                    strJson = strJson + ",\"orderId\":\"\"}";
                }
                if (!strJson.Contains("\"requestId\":"))
                {
                    strJson = strJson.Remove(strJson.Length - 1);
                    strJson = strJson + ",\"requestId\":\"\"}";
                }
                if (!strJson.Contains("\"count\":"))
                {
                    strJson = strJson.Remove(strJson.Length - 1);
                    strJson = strJson + ",\"count\":-999}";
                }
                if (!strJson.Contains("\"msg\":"))
                {
                    strJson = strJson.Remove(strJson.Length - 1);
                    strJson = strJson + ",\"msg\":\"\"}";
                }

                DataContractJsonSerializer dataConSer = new DataContractJsonSerializer(typeof(OrderWaitTime));
                using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(strJson)))
                {
                    OrderWaitTime orderWaitTime = dataConSer.ReadObject(ms) as OrderWaitTime;
                    return orderWaitTime;
                }
            }
            catch
            {
                return null;
            }
        }

        public static TrainInfo[] ToTrainInfoArray(string strJson) 
        {
            try
            {
                if (strJson == null || strJson == "")
                {
                    return null;
                }

                DataContractJsonSerializer dataConSer = new DataContractJsonSerializer(typeof(TrainInfo[]));
                using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(strJson)))
                {
                    TrainInfo[] trainInfoArray = dataConSer.ReadObject(ms) as TrainInfo[];
                    return trainInfoArray;
                }
            }
            catch
            {
                return null;
            }
        }
    }
}
